<?php
// +-------------------------------------------------------------------------------
// | DingZone [ A Rapid Development Framework For Dingtalk ]
// +-------------------------------------------------------------------------------
// | 企业 v1.0
// |
// | Copyright (c) 2014-2018 http://www.heimalanshi.com All rights reserved.
// | Author : Jie
// | Date : 2018-08-09
// +-------------------------------------------------------------------------------

namespace app\core\model;

use think\Db;

class Enterprise {
    /**
     * 获取查询对象
     *
     * @return \think\db\Query
     */
    protected static function _q() {
        return Db::name("enterprise");
    }

    /**
     * 新增
     *
     * @param $data
     * @return int|string
     */
    public static function add($data) {
        return self::_q()->insertGetId($data);
    }

    /**
     * 删除企业
     *
     * @param $eid
     * @return int
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function delete($eid) {
        return self::_q()->where(['id' => $eid])->delete();
    }

    /**
     * 获取单条记录
     *
     * @param $eid
     * @return array|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function get($eid) {
        return self::_q()->where('id', $eid)->find();
    }

    /**
     * 获取callback配置
     *
     * @param $eid
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getCallback($eid) {
        $enterprise = self::get($eid);
        $callback = $enterprise['callback'];
        if (!$callback) {
            $callback = [];
        }
        if (is_string($callback)) {
            $callback = json_decode($callback, true);
        }
        if (!isset($callback['open'])) {
            $callback['open'] = false;
        }
        if (!isset($callback['tags'])) {
            $callback['tags'] = [];
        }
        return $callback;
    }

    /**
     * 批量获取
     *
     * @param $eids
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function batchGet($eids) {
        return self::_q()->where('id', 'in', $eids)->select();
    }

    /**
     * 查询全部记录
     *
     * @param array $condition
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function all($condition = []) {
        return self::_q()->where($condition)->select();
    }

    /**
     * 更新数据
     *
     * @param $data
     * @param $eid
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function update($data, $eid) {
        return self::_q()->where('id', $eid)->update($data);
    }

    /**
     * 解析登录类型
     *
     * @param $enterprise
     * @return array|mixed
     */
    public static function parseLoginType($enterprise) {
        if (empty($enterprise)) {
            return [];
        }
        $loginType = json_decode($enterprise['login_type'], true);
        if (!isset($loginType['ding'])) {
            $loginType['ding'] = true;
        }
        if (!isset($loginType['mobile'])) {
            $loginType['mobile'] = true;
        }
        if (!isset($loginType['username'])) {
            $loginType['username'] = true;
        }
        return $loginType;
    }

    /**
     * 解析短信配置
     *
     * @param $enterprise
     * @return array
     */
    public static function parseSmsconfig($enterprise) {
        if (empty($enterprise)) {
            return [];
        }
        $smsConfig = $enterprise['smsconfig'];
        if (!empty($smsConfig)) {
            $smsConfig = json_decode($smsConfig, true);
        } else {
            $smsConfig = [];
        }
        return $smsConfig;
    }

    /**
     * 解析企业首页配置
     *
     * @param $enterprise
     * @return array|mixed
     */
    public static function parseHomepage($enterprise) {
        $homePage = $enterprise['home_page'];
        $homePage = empty($homePage) ? [] : json_decode($homePage, true);
        if (!isset($homePage['pc'])) {
            $homePage['pc'] = '';
        }
        if (!isset($homePage['mobile'])) {
            $homePage['mobile'] = '';
        }
        if (!isset($homePage['dingPc'])) {
            $homePage['dingPc'] = '';
        }
        if (!isset($homePage['dingMobile'])) {
            $homePage['dingMobile'] = '';
        }
        return $homePage;
    }

    /**
     * 判断当前企业是否开启隐私模式
     *
     * @param $eid
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function isPrivacyModeOn($eid) {
        $privacyMode = EnterpriseCustomize::getContent($eid, EnterpriseCustomize::TYPE_CONFIG, "*/*/*/*/PRIVACY_MODE", false);
        if ($privacyMode == 'true' || $privacyMode == 'on') {
            return true;
        }
        return false;
    }
}